home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / DOSFG.C < prev    next >
C/C++ Source or Header  |  1992-05-08  |  11KB  |  412 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/src/microcode/RCS/dosfg.c,v 1.2 1992/05/08 19:05:26 mhwu Exp $
  4.  
  5. Copyright (c) 1992 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* Zortech 'Flash Graphics' primitives */
  36.  
  37. #include "scheme.h"
  38. #include "prims.h"
  39. #include <fg.h>
  40.  
  41. #define ARG_FG_COORD    arg_nonnegative_integer
  42.  
  43. /* Internal state to emulate Starbase behaviour */
  44. typedef struct fg_state_struct
  45. {
  46.   fg_color_t color;
  47.   fg_color_t background;
  48.   fg_coord_t x;
  49.   fg_coord_t y;
  50.   fg_box_t clip;
  51.   int mode;
  52.   int line_type;
  53.   int mask;
  54.   int rotation;
  55. } fg_state_t;
  56.  
  57. static fg_state_t current;
  58.  
  59. /* This makes sure that the variables will not be changed unless
  60.    both args are of the right type.
  61.  */
  62.  
  63. #define Get_Scheme_Coordinates(xp, x, yp, y)        \
  64. do                            \
  65. { fg_coord_t _tmp_x, _tmp_y;                \
  66.   _tmp_x = (ARG_FG_COORD(xp));                \
  67.   _tmp_y = (ARG_FG_COORD(yp));                \
  68.   (x) = _tmp_x, (y) = _tmp_y;                \
  69. } while (0)
  70.  
  71.  
  72. DEFINE_PRIMITIVE ("FG-OPEN", Prim_fg_open, 0, 0,
  73. "Initializes the graphics display.")
  74. {
  75.   PRIMITIVE_HEADER (0);
  76.   {
  77.     if (fg_init() != 0)
  78.     {
  79.       current.color    = FG_WHITE;
  80.       current.background= FG_BLACK;
  81.       current.line_type    = FG_LINE_SOLID;
  82.       current.mode     = FG_MODE_SET;
  83.       current.rotation    = FG_ROT0;
  84.       current.mask    = (int) (~0);
  85.       current.x     = (fg_coord_t) 0;
  86.       current.y     = (fg_coord_t) 0;
  87.       
  88.       fg_box_cpy(current.clip, fg.displaybox);
  89.  
  90.       { int i;
  91.     for (i=1000000L; i > 0; i--);
  92.       }
  93.  
  94.       PRIMITIVE_RETURN (SHARP_T);
  95.     }
  96.     else
  97.       PRIMITIVE_RETURN (SHARP_F);
  98.   }
  99. }
  100.  
  101. DEFINE_PRIMITIVE ("FG-CLOSE", Prim_fg_close, 0, 0,
  102. "Closes the graphics device.")
  103. {
  104.   PRIMITIVE_HEADER (0);
  105.   fg_term();
  106.   PRIMITIVE_RETURN (UNSPECIFIC);
  107. }
  108.  
  109. DEFINE_PRIMITIVE ("FG-CLEAR", Prim_fg_clear, 0, 0, 0)
  110. {
  111.   PRIMITIVE_HEADER (0);
  112.   fg_fillbox(current.background, current.mode, current.mask, fg.displaybox);
  113.   PRIMITIVE_RETURN (UNSPECIFIC);
  114. }
  115.  
  116. DEFINE_PRIMITIVE ("FG-DRAW-POINT", Prim_fg_draw_point, 2, 2, 0)
  117. {
  118.   PRIMITIVE_HEADER (2);
  119.   { 
  120.     Get_Scheme_Coordinates(1, current.x, 2, current.y);
  121.  
  122.     if (fg_pt_inbox(current.clip, current.x, current.y))
  123.       fg_drawdot(current.color, current.mode, current.mask,
  124.          current.x, current.y);
  125.   }
  126.   PRIMITIVE_RETURN (UNSPECIFIC);
  127. }
  128.  
  129. DEFINE_PRIMITIVE ("FG-MOVE-CURSOR", Prim_fg_move_cursor, 2, 2, 0)
  130. {
  131.   PRIMITIVE_HEADER (2);
  132.   {     
  133.     Get_Scheme_Coordinates(1, current.x, 2, current.y);
  134.   }
  135.   PRIMITIVE_RETURN (UNSPECIFIC);
  136. }
  137.  
  138. DEFINE_PRIMITIVE ("FG-DRAG-CURSOR", Prim_fg_drag_cursor, 2, 2, 0)
  139. {
  140.   PRIMITIVE_HEADER (2);
  141.   { fg_coord_t x, y;
  142.     fg_line_t line, cline;
  143.  
  144.     Get_Scheme_Coordinates(1, x, 2, y);
  145.  
  146.     fg_make_line(line, current.x, current.y, x, y);
  147.     (void) fg_lineclip(current.clip, line, cline);
  148.     fg_drawline(current.color, current.mode, current.mask,
  149.         current.line_type, cline);
  150.     current.x = x, current.y = y;
  151.   }
  152.   PRIMITIVE_RETURN (UNSPECIFIC);
  153. }
  154.  
  155. DEFINE_PRIMITIVE ("FG-DRAW-LINE", Prim_fg_draw_line, 4, 4, 0)
  156. {
  157.   PRIMITIVE_HEADER (4);
  158.   { fg_coord_t x, y;
  159.     fg_line_t line, cline;
  160.  
  161.     Get_Scheme_Coordinates(1, x, 2, y);
  162.     Get_Scheme_Coordinates(3, current.x, 4, current.y);
  163.  
  164.     fg_make_line(line, x, y, current.x, current.y);
  165.     (void) fg_lineclip(current.clip, line, cline);
  166.     fg_drawline(current.color, current.mode, current.mask,
  167.         current.line_type, cline);
  168.   }
  169.   PRIMITIVE_RETURN (UNSPECIFIC);
  170. }
  171.  
  172. DEFINE_PRIMITIVE ("FG-SET-LINE-STYLE", Prim_fg_set_line_style, 1, 1, 0)
  173. {
  174.   PRIMITIVE_HEADER (1);
  175.   current.line_type = (int) arg_index_integer(1, FG_LINE_MAX);
  176.   PRIMITIVE_RETURN (UNSPECIFIC);
  177. }
  178.  
  179. DEFINE_PRIMITIVE ("FG-SET-DRAWING-MODE", Prim_fg_set_drawing_mode, 1, 1, 0)
  180. {
  181.   PRIMITIVE_HEADER (1);
  182.   { int mode = (int) arg_index_integer(1, 2);
  183.     
  184.     current.mode = (mode == 0) ? FG_MODE_SET : FG_MODE_XOR;
  185.   }
  186.   PRIMITIVE_RETURN (UNSPECIFIC);
  187. }
  188.  
  189. DEFINE_PRIMITIVE ("FG-DEVICE-COORDINATES", Prim_fg_device_coordinates, 0, 0, 0)
  190. {
  191.   PRIMITIVE_HEADER (0);
  192.   { SCHEME_OBJECT result = (allocate_marked_vector (TC_VECTOR, 4, true));
  193.  
  194.     VECTOR_SET (result, 0, long_to_integer(fg.displaybox[FG_X1]));
  195.     VECTOR_SET (result, 1, long_to_integer(fg.displaybox[FG_Y1]));
  196.     VECTOR_SET (result, 2, long_to_integer(fg.displaybox[FG_X2])); 
  197.     VECTOR_SET (result, 3, long_to_integer(fg.displaybox[FG_Y2]));
  198.  
  199.     PRIMITIVE_RETURN (result);
  200.   }
  201. }
  202.  
  203. DEFINE_PRIMITIVE ("FG-RESET-CLIP-RECTANGLE", Prim_fg_reset_clip_rectangle,
  204.           0, 0, 0)
  205. {
  206.   PRIMITIVE_HEADER (0);
  207.   {
  208.     fg_box_cpy(current.clip, fg.displaybox);
  209.   }
  210.   PRIMITIVE_RETURN (UNSPECIFIC);
  211. }
  212.  
  213. DEFINE_PRIMITIVE ("FG-SET-CLIP-RECTANGLE", Prim_fg_set_clip_rectangle, 4, 4, 0)
  214. {
  215.   PRIMITIVE_HEADER (4);
  216.   { fg_coord_t x1, y1, x2, y2;
  217.     
  218.     Get_Scheme_Coordinates(1, x1, 2, y1);
  219.     Get_Scheme_Coordinates(1, x2, 2, y2);
  220.  
  221.     if (fg_pt_inbox(fg.displaybox, x1, y1) == 0)
  222.       error_bad_range_arg(1);
  223.     if (fg_pt_inbox(fg.displaybox, x1, y1) == 0)
  224.       error_bad_range_arg(3);
  225.     
  226.     current.clip[FG_X1] = x1;
  227.     current.clip[FG_Y1] = y1;
  228.     current.clip[FG_X2] = x2;
  229.     current.clip[FG_Y2] = y2;
  230.   }
  231.   PRIMITIVE_RETURN (UNSPECIFIC);
  232. }
  233.  
  234. DEFINE_PRIMITIVE ("FG-DRAW-TEXT", Prim_fg_draw_text, 3, 3,
  235.   "(FG-DRAW-TEXT DEVICE X Y STRING)")
  236. {
  237.   PRIMITIVE_HEADER (3);
  238.   {
  239.     fg_coord_t x, y;
  240.     unsigned char * string = STRING_ARG(3);
  241.  
  242.     Get_Scheme_Coordinates(1, x, 2, y);
  243.     fg_puts(current.color, current.mode, current.mask,
  244.         current.rotation, x, y, string, current.clip);
  245.   }    
  246.   PRIMITIVE_RETURN (UNSPECIFIC);
  247. }
  248.  
  249. DEFINE_PRIMITIVE ("FG-SET-TEXT-ROTATION", Prim_fg_set_text_rotation, 1, 1, 0)
  250. {
  251.   PRIMITIVE_HEADER (1);
  252.   {
  253.     long angle = ((long) (arg_real_number (1))) % 360;
  254.     int path_style = FG_ROT0;
  255.  
  256.     if ((angle > 315) || (angle <=  45))
  257.       path_style = FG_ROT0;
  258.     else if ((angle > 45) && (angle <= 135))
  259.       path_style = FG_ROT90;
  260.     else if ((angle > 135) && (angle <= 225))
  261.       path_style = FG_ROT180;
  262.     else if ((angle > 225) && (angle <= 315))
  263.       path_style = FG_ROT270;
  264.     
  265.     current.rotation = path_style;
  266.   }
  267.   PRIMITIVE_RETURN (UNSPECIFIC);
  268. }
  269.  
  270. DEFINE_PRIMITIVE ("FG-COLOR-MAP-SIZE", Prim_fg_color_map_size, 0, 0, 0)
  271. {
  272.   PRIMITIVE_HEADER (0);
  273.   PRIMITIVE_RETURN (long_to_integer (fg.ncolormap));
  274. }
  275.  
  276. DEFINE_PRIMITIVE ("FG-DEFINE-COLOR", Prim_fg_define_color, 4, 4,
  277.   "(FG-DEFINE-COLOR COLOR-INDEX RED GREEN BLUE)")
  278. {
  279.   PRIMITIVE_HEADER (4);
  280.   {
  281.     fg_color_t index, r, g, b;
  282.     
  283.     index = arg_index_integer(1, fg.ncolormap);
  284.     r = arg_real_number(2);
  285.     g = arg_real_number(3);
  286.     b = arg_real_number(4);
  287.  
  288.     fg_setpalette(index, r, g, b);
  289.   }
  290.   PRIMITIVE_RETURN (UNSPECIFIC);
  291. }
  292.  
  293. DEFINE_PRIMITIVE ("FG-SET-LINE-COLOR", Prim_fg_set_line_color, 1, 1,
  294.   "(FG-SET-LINE-COLOR COLOR-INDEX)")
  295. {
  296.   PRIMITIVE_HEADER (1);
  297.   current.color = arg_index_integer(1, fg.ncolormap);
  298.   PRIMITIVE_RETURN (UNSPECIFIC);
  299. }
  300.  
  301. #if 0
  302. /* Graphics Screen Dump */
  303.  
  304. static void print_graphics ();
  305.  
  306. UNDEFINE_PRIMITIVE ("FG-WRITE-IMAGE-FILE", Prim_fg_write_image_file, 3, 3,
  307.   "(FG-WRITE-IMAGE-FILE DEVICE FILENAME INVERT?)\n\
  308. Write a file containing an image of the DEVICE's screen, in a format\n\
  309. suitable for printing on an HP laserjet printer.\n\
  310. If INVERT? is not #F, invert black and white in the output.")
  311. {
  312.   PRIMITIVE_HEADER (3);
  313.   print_graphics ((SB_DEVICE_ARG (1)), (STRING_ARG (2)), (BOOLEAN_ARG (3)));
  314.   PRIMITIVE_RETURN (UNSPECIFIC);
  315. }
  316.  
  317. static char rasres[] = "\033*t100R";
  318. static char rastop[] = "\033&l2E";
  319. static char raslft[] = "\033&a2L";
  320. static char rasbeg[] = "\033*r0A";
  321. static char raslen[] = "\033*b96W";
  322. static char rasend[] = "\033*rB";
  323.  
  324. static int
  325. inquire_cmap_mask (fildes)
  326.      int fildes;
  327. {
  328.   int cmap_size = (inquire_cmap_size (fildes));
  329.   return
  330.     (((cmap_size >= 0) && (cmap_size < 8))
  331.      ? ((1 << cmap_size) - 1)
  332.      : (-1));
  333. }
  334.  
  335. static int
  336. open_dumpfile (dumpname)
  337.   char * dumpname;
  338. {
  339.   int dumpfile = (creat (dumpname, 0666));
  340.   if (dumpfile == (-1))
  341.     {
  342.       fprintf (stderr, "\nunable to create graphics dump file.");
  343.       fflush (stderr);
  344.       error_external_return ();
  345.     }
  346.   dumpfile = (open (dumpname, OUTINDEV));
  347.   if (dumpfile == (-1))
  348.     {
  349.       fprintf (stderr, "\nunable to open graphics dump file.");
  350.       fflush (stderr);
  351.       error_external_return ();
  352.     }
  353.   return (dumpfile);
  354. }
  355.  
  356. static void
  357. print_graphics (descriptor, dumpname, inverse_p)
  358.      int descriptor;
  359.      char * dumpname;
  360.      int inverse_p;
  361. {
  362.   int dumpfile = (open_dumpfile (dumpname));
  363.   write (dumpfile, rasres, (strlen (rasres)));
  364.   write (dumpfile, rastop, (strlen (rastop)));
  365.   write (dumpfile, raslft, (strlen (raslft)));
  366.   write (dumpfile, rasbeg, (strlen (rasbeg)));
  367.   {
  368.     fast unsigned char mask = (inquire_cmap_mask (descriptor));
  369.     int col;
  370.     for (col = (1024 - 16); (col >= 0); col = (col - 16))
  371.       {
  372.     unsigned char pixdata [(16 * 768)];
  373.     {
  374.       fast unsigned char * p = (& (pixdata [0]));
  375.       fast unsigned char * pe = (& (pixdata [sizeof (pixdata)]));
  376.       while (p < pe)
  377.         (*p++) = '\0';
  378.     }
  379.     dcblock_read (descriptor, col, 0, 16, 768, pixdata, 0);
  380.     {
  381.       int x;
  382.       for (x = (16 - 1); (x >= 0); x -= 1)
  383.         {
  384.           unsigned char rasdata [96];
  385.           fast unsigned char * p = (& (pixdata [x]));
  386.           fast unsigned char * r = rasdata;
  387.           int n;
  388.           for (n = 0; (n < 96); n += 1)
  389.         {
  390.           fast unsigned char c = 0;
  391.           int nn;
  392.           for (nn = 0; (nn < 8); nn += 1)
  393.             {
  394.               c <<= 1;
  395.               if (((* p) & mask) != 0)
  396.             c |= 1;
  397.               p += 16;
  398.             }
  399.           (*r++) = (inverse_p ? (~ c) : c);
  400.         }
  401.           write (dumpfile, raslen, (strlen (raslen)));
  402.           write (dumpfile, rasdata, 96);
  403.         }
  404.     }
  405.       }
  406.   }
  407.   write (dumpfile, rasend, (strlen (rasend)));
  408.   close (dumpfile);
  409.   return;
  410. }
  411. #endif
  412.